package com.hankol.winecellar.fragment

import androidx.lifecycle.Observer
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseLazyFragment
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleClickHelper
import com.baselibrary.utils.EventBusUtils
import com.baselibrary.utils.SpUtils
import com.hankol.winecellar.R
import com.hankol.winecellar.adapter.GoodsManagerAdapter
import com.hankol.winecellar.bean.GoodsManagerListBean
import com.hankol.winecellar.databinding.FragmentGoodsManagerListBinding
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.winecellar.viewmodel.WineCellarViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 【商品管理】
 * 售卖中
 * 已下架
 *  商品列表
 *
 *  status: 类型：0售卖中，1已下架
 * @author: merbng
 * @time: 2022/9/4 15:34
 * @description:
 */
class GoodsManagerListFragment(val status: Int) : BaseLazyFragment(false) {
    private lateinit var binding: FragmentGoodsManagerListBinding
    private lateinit var viewModel: WineCellarViewModel

    var page = 1
    private var hasnext: Boolean = false

    private val mAdapter by lazy { GoodsManagerAdapter() }

    companion object {
        fun newInstance(status: Int = -1): GoodsManagerListFragment {
            return GoodsManagerListFragment(status)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_goods_manager_list
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        viewModel = getFragmentViewModelProvider(this).get(WineCellarViewModel::class.java)
        binding = getDataBinding<FragmentGoodsManagerListBinding>()
        initView()
        initListener()
        initObserver()
    }

    fun initView() {
        binding.rvGoodList.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val goodsManagerListBean = adapter.data[position] as GoodsManagerListBean
            if (!DoubleClickHelper.isOnDoubleClick()) {
                val hashMap = java.util.HashMap<String, Any>()
                hashMap[Constants.GOODS_DETAIL_ID] = goodsManagerListBean.spuId
                hashMap[Constants.GOODS_DETAIL_SOURCE] = "经营者商品管理"
                ARouterUtils.jump(ActivityRouter.NEW_RETAIL.GOODS_DETAUILS, hashMap)
            }
        }
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val bean = adapter.data[position] as GoodsManagerListBean
            if (!DoubleClickHelper.isOnDoubleClick()) {
                //排序
                com.hankol.winecellar.utils.PopUtils.showGoodsSortDialog(requireActivity(), bean.sort, object : com.hankol.winecellar.utils.PopUtils.IGoodsSortCallback {
                    override fun onClickOk(num: String) {
                        viewModel.reqGoodsModifySort(num, bean.spuId)
                    }
                })
            }
        }
    }

    fun initListener() {
        binding.smartRefresh.setOnRefreshListener {
            EventBusUtils.sendNotify(MessageBus.msgId_refresh_goods_manager)
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
    }

    fun initObserver() {
        viewModel.goodsManagerListLD.observe(this, Observer {
            hideLoadingDialog()
            binding.smartRefresh.finishRefresh()
            if (it.data.size == 0) {
                //无数据
                if (page != 1) {
                    binding.smartRefresh.finishLoadMore()
                    return@Observer
                }
                mAdapter.setNewData(null)
                mAdapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_EVALUATION_LIST, binding.rvGoodList, "暂无商品")
            } else {
                if (it.data.checkNotNull()) {
                    hasnext = it.totalPage != mAdapter.data.size
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        mAdapter.setNewData(it.data)
                    } else {
                        mAdapter.addData(it.data)
                    }
                    page++
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            }
        })
        viewModel.changeSortLD.observe(this, Observer {
            page = 1
            loadData()
        })
    }

    override fun lazyLoadData() {
        page = 1
        loadData()
    }

    fun loadData() {
        viewModel.requestGoodsManagerList(status, SpUtils.getInstance().currentWineryInfo.dealerCode, page)
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        when (messageBus.codeType) {

        }
    }

    override fun onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
        super.onDestroy()
    }
}